<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>点击div</div>
    <script>
        var div = document.querySelector("div");
        div.onclick = function(){
            var xhr = new XMLHttpRequest();
            // xhr.open("get","ajax.php?username=aaa");
            // xhr.send();
            xhr.open("post","ajax.php");
            // 在请求头中设置编码格式
            xhr.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");
            // 在请求头内添加内容
            // xhr.setRequestHeader('aaaaaaaa',"bbbbbbb");
            
            // post请求是数据是在send中传递的
            xhr.send("username=aaa");
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    console.log(xhr.responseText);
                    var data= JSON.parse(xhr.responseText);
                    console.log(data);
                }
            }
        }
    </script>
</body>
</html>